package com.fresh.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fresh.entity.ShopEntity;
import com.fresh.entity.UserEntity;
import com.fresh.entity.UserPermissionEntity;
import com.fresh.service.IShopService;
import com.fresh.service.UserPermissionService;
import com.fresh.service.UserService;
import com.fresh.service.VerifyService;
import com.fresh.utils.Constant;
import com.fresh.utils.MD5;
import com.fresh.utils.PageModel;
import com.fresh.utils.Response;


/**
 * @ClassName: AuthorityController
 * @Description: 权限管理
 * @author: LJY
 * @date: 2017年10月13日 下午7:52:54
 */
@RequestMapping("/permission")
@Controller
public class AuthorityController extends BaseController {

	@Autowired 
	private  UserService  userService; 
	@Autowired 
	private  UserPermissionService  userPermissionService; 
	@Autowired 
	private IShopService shopService;
	
	
	@Autowired 
	private VerifyService verifyService;
	
	/**
	 * @throws IOException   
	* @Title: toAuthority  
	* @Description: 进入权限管理页面
	* @param request
	* @param model
	* @return String  
	* @throws  
	*/ 
	@RequestMapping(value = "/authority")
	public String toAuthority(HttpServletRequest request , Model model,HttpServletResponse response) throws IOException  {
		
		//权限拦截
		verifyService.permissionIntercept(Constant.AUTHORITY_PER, response, request);
		
		// 获取商户ID查询用户信息 
		String shopId = request.getSession().getAttribute("SHOP_ID").toString();
		List<UserEntity> userLlist = userService.queryList(shopId);
		ShopEntity shop = shopService.getShopSearchNumber(shopId);
		request.setAttribute("shop", shop);
		
		// 当前页码和显示条数
		int currentPage=1,size=10;
		// 页面上的页码
		String pageNo = request.getParameter("pageNo"); 
		if(pageNo!=null && !"".equals(pageNo)){
			currentPage=Integer.parseInt(pageNo);
		}
		//调用业务层方法
		PageModel<UserEntity> pm = userService.queryUserPage(currentPage, size, shopId);

		request.setAttribute("pm", pm);
		return "/permission/authority";
	}
	

	/**  
	* @Title: toAddUser  
	* @Description: 添加用户 
	* @param name,phone,roleName,menuIds,serviceIds
	* @param request
	* @return Response  
	* @throws  
	*/ 
	@PostMapping("addUser")
	@ResponseBody
	public Response toAddUser(String name,String phone,String roleName,String menuIds,String serviceIds,HttpServletRequest request )  {
		
		// 获取商品ID
		String shopId = request.getSession().getAttribute("SHOP_ID").toString();
		
		List<UserEntity> list = userService.findByName(name);
		if (list.size()<=0) {
			UserEntity  userEntity = new  UserEntity();
			userEntity.setName(name);
			userEntity.setPhone(phone);
			userEntity.setRoleName(roleName);
			userEntity.setShopId(shopId);
			userEntity.setType(1);
			userEntity.setPwd(MD5.md5("123456"));
			
			userService.save(userEntity,menuIds,serviceIds);
			return Response.ok();
		}
		
		
		return Response.error(0, "用户名已存在！");
	}
	

	/**  
	* @Title: getUserPermission  
	* @Description: 获取用户的权限 
	* @param id
	* @param request
	* @return Response  
	* @throws  
	*/ 
	@PostMapping("getUserPermission")
	@ResponseBody
	public Response getUserPermission(String id, HttpServletRequest request )  {
		
		String shopId = request.getSession().getAttribute("SHOP_ID").toString();
        String userId = request.getSession().getAttribute("USER_ID").toString();
        UserEntity user = userService.findByType(shopId);
        
        // 服务list
        List<UserPermissionEntity> servicePermissionList =  userPermissionService.queryUser(userId, UserPermissionEntity.TYPE_SERVICE_0);
        // 菜单list
        List<UserPermissionEntity> menuPermissionList =  userPermissionService.queryUser(userId, UserPermissionEntity.TYPE_SERVICE_1);
        
        // 我的服务list
        List<UserPermissionEntity> myServicePermissionList =  userPermissionService.queryUser(id, UserPermissionEntity.TYPE_SERVICE_0);
        // 我的菜单list
        List<UserPermissionEntity> myMenuPermissionList =  userPermissionService.queryUser(id, UserPermissionEntity.TYPE_SERVICE_1);
        
        HashMap<String , Object>  hashMap = new HashMap<String , Object>();
        request.setAttribute("user", user);
        hashMap.put("servicePermissionList", servicePermissionList);
        hashMap.put("menuPermissionList", menuPermissionList);
        hashMap.put("myServicePermissionList", myServicePermissionList);
        hashMap.put("myMenuPermissionList", myMenuPermissionList);
        
		return Response.ok(hashMap);  
	    
	}
	
	/**
     * 编辑修改用户
     * @return
     */
	/**  
	* @Title: toEditUser  
	* @Description: 编辑修改用户 
	* @param name,phone,roleName,menuIds,id,serviceIds
	* @return Response  
	* @throws  
	*/ 
	@PostMapping("editUser")
	@ResponseBody
	public Response toEditUser(String name,String phone,  String  roleName  ,String menuIds,String id,String serviceIds)  {
		
		UserEntity  userEntity = userService.findById(id);
		userEntity.setName(name);
		userEntity.setRoleName(roleName);
		userEntity.setPhone(phone);
		
		userService.updateUser(userEntity,menuIds,serviceIds);
		
		return Response.ok();
	}
	
	/**  
	* @Title: toDeletUser  
	* @Description: 删除用户
	* @param @param id
	* @return Response  
	* @throws  
	*/ 
	@PostMapping("deleteUser")
	@ResponseBody
	public Response toDeletUser(String id)  {
		userService.deleteUser(id);
		
		return Response.ok();
	}
	
	/**  
	* @Title: findById  
	* @Description: 根据id查询用户
	* @param id
	* @return Response  
	* @throws  
	*/ 
	@PostMapping("findById")
	@ResponseBody
	public Response findById(String id)  {
	
		UserEntity   userEntity = userService.findById(id);
		
		HashMap<String, Object>  hashMap  = new  HashMap<String, Object>();
		hashMap.put("user", userEntity);
		
		return Response.ok(hashMap);
	}
	
	
}
